package top.v5it.japi.plus.core.spring.web;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import top.v5it.japi.plus.common.func.Func1;
import top.v5it.japi.plus.common.tuple.Tuple3;
import top.v5it.japi.plus.common.tuple.Tuples;
import top.v5it.japi.plus.core.util.JapiSecureUtil;

/**
 * 加解密算法
 *
 * @author zhanpu
 * @date 2022/6/17
 */
@Getter
@RequiredArgsConstructor
public enum EadaAlgorithm {

    /**
     * RSA加解密
     */
    RSA(JapiSecureUtil::rsaDecrypt, JapiSecureUtil::rsaEncrypt),

    /**
     * AES加解密
     */
    AES(JapiSecureUtil::aesDecrypt, JapiSecureUtil::aesEncrypt);

    /**
     * 解密
     */
    private final Func1<String, String, RuntimeException> decrypt;

    /**
     * 加密
     */
    private final Func1<String, String, RuntimeException> encrypt;

    /**
     * 按参获取加解密函数
     *
     * @param isEada 是否加解密
     * @return {@link Func1<String, String, RuntimeException>}
     */
    public Func1<String, String, RuntimeException> getEadaFunc(boolean isEada) {
        return isEada ? encrypt : decrypt;
    }

    /**
     * 按参数获取函数
     *
     * @param isEada   是否加解密
     * @param japiEada {@link JapiEada}
     * @return {@link Tuple3 <Boolean, JapiEada, Func1<String, String, RuntimeException>>}
     */
    public Tuple3<Boolean, JapiEada, Func1<String, String, RuntimeException>> getEadaMapper(boolean isEada, JapiEada japiEada) {
        return Tuples.of(isEada, japiEada, getEadaFunc(isEada));
    }

}
